package com.lexue.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lexue.dao.IMessage;
import com.lexue.domain.Message;
import com.lexue.domain.TeacherInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Message entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.lexue.dao.impl.Message
 * @author MyEclipse Persistence Tools
 */

public class MessageDAO extends HibernateDaoSupport implements IMessage {
	private static final Log log = LogFactory.getLog(MessageDAO.class);
	// property constants
	public static final String SENDER = "sender";
	public static final String SENDER_EMAIL = "senderEmail";
	public static final String SENDER_PHONE = "senderPhone";
	public static final String SENDER_MPHONE = "senderMphone";
	public static final String SENDER_ADDRESS = "senderAddress";
	public static final String REQUIRE_DETAIL = "requireDetail";
	public static final String IS_READ = "isRead";
	public static final TeacherInfo TEACHER_INFO = new TeacherInfo();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#save(com.lexue.domain.Message)
	 */
	public void save(Message transientInstance) {
		log.debug("saving Message instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#delete(com.lexue.domain.Message)
	 */
	public void delete(Message persistentInstance) {
		log.debug("deleting Message instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findById(java.lang.Integer)
	 */
	public Message findById(java.lang.Integer id) {
		log.debug("getting Message instance with id: " + id);
		try {
			Message instance = (Message) getHibernateTemplate().get(
					"com.lexue.domain.Message", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findByExample(com.lexue.domain.Message)
	 */
	public List findByExample(Message instance) {
		log.debug("finding Message instance by example");
		try {
			List results = getHibernateTemplate().getSessionFactory()
					.openSession().createCriteria("com.lexue.domain.Message")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Message instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Message as model where model."
					+ propertyName + "= ?";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findbyTeacherID(TeacherInfo teacher) {
		String queryString = "from Message as model where model.teacherInfo"
				+ "= ?";
		Query queryObject = getHibernateTemplate().getSessionFactory()
				.openSession().createQuery(queryString);
		queryObject.setParameter(0, teacher);
		return queryObject.list();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findBySender(java.lang.Object)
	 */
	public List findBySender(Object sender) {
		return findByProperty(SENDER, sender);
	}


	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findBySenderEmail(java.lang.Object)
	 */
	public List findBySenderEmail(Object senderEmail) {
		return findByProperty(SENDER_EMAIL, senderEmail);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findBySenderPhone(java.lang.Object)
	 */
	public List findBySenderPhone(Object senderPhone) {
		return findByProperty(SENDER_PHONE, senderPhone);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findBySenderMphone(java.lang.Object)
	 */
	public List findBySenderMphone(Object senderMphone) {
		return findByProperty(SENDER_MPHONE, senderMphone);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findBySenderAddress(java.lang.Object)
	 */
	public List findBySenderAddress(Object senderAddress) {
		return findByProperty(SENDER_ADDRESS, senderAddress);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findByRequireDetail(java.lang.Object)
	 */
	public List findByRequireDetail(Object requireDetail) {
		return findByProperty(REQUIRE_DETAIL, requireDetail);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findByIsRead(java.lang.Object)
	 */
	public List findByIsRead(Object isRead) {
		return findByProperty(IS_READ, isRead);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#findAll()
	 */
	public List findAll() {
		log.debug("finding all Message instances");
		try {
			String queryString = "from Message";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#merge(com.lexue.domain.Message)
	 */
	public Message merge(Message detachedInstance) {
		log.debug("merging Message instance");
		try {
			Message result = (Message) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#attachDirty(com.lexue.domain.Message)
	 */
	public void attachDirty(Message instance) {
		log.debug("attaching dirty Message instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.IMessage#attachClean(com.lexue.domain.Message)
	 */
	public void attachClean(Message instance) {
		log.debug("attaching clean Message instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}